package me.xplabs.model.proxy 
{
	import avmplus.getQualifiedClassName;
	import com.greensock.events.LoaderEvent;
	import flash.utils.ByteArray;
	import me.xplabs.Globals;
	import me.xplabs.astar.AStar;
	import me.xplabs.constant.ResType;
	import me.xplabs.constant.SceneType;
	import me.xplabs.interfaces.IAstarFindPath;
	import me.xplabs.model.BuildDataManager;
	import me.xplabs.model.combatunit.BaseBuff;
	import me.xplabs.model.combatunit.MFlagInfo;
	import me.xplabs.model.FlagDataManager;
	import me.xplabs.model.RoleDataManager;
	import me.xplabs.model.combatunit.BaseBuild;
	import me.xplabs.model.combatunit.BaseFlag;
	import me.xplabs.model.combatunit.BaseRole;
	import me.xplabs.model.energy.EnergyData;
	import me.xplabs.model.map.MMapData;
	import me.xplabs.model.msg.MsgHitActor;
	import me.xplabs.model.skill.BaseSkill;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.msg.NotificationRaidInfo;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	/**
	 * 地图数据管理
	 * ...
	 * @author xiaohan
	 */
	public class MMapManagerProxy extends Proxy 
	{
		public static var NAME:String;
		public var isUpdate:Boolean;
		public var raidId:int;
		/** 是否是最后一波敌人都已经出来*/
		public var isLastWave:Boolean;
		/** 是否已经发送了游戏结束的消息*/
		public var isSendEndGame:Boolean;
		
		private var _mMapData:MMapData;
		private var _aStar:AStar;
		private var _buildDataManager:BuildDataManager;
		private var _flagDataManager:FlagDataManager;
		private var _roleDataManager:RoleDataManager;
		public function MMapManagerProxy() 
		{
			NAME = getQualifiedClassName(this);
			super(NAME, null);
			_mMapData = new MMapData();
			_aStar = new AStar(_mMapData, 3000);
			
			_roleDataManager = new RoleDataManager();
			_buildDataManager = new BuildDataManager();
			_flagDataManager = new FlagDataManager();
			
			startUpdate();
		}
		/**
		 * 开启数据刷新
		 */
		public function startUpdate():void
		{
			if (isUpdate) return;
			isUpdate = true;
			Globals.gameManager.addDataUpdate(update);
		}
		/**
		 * 关闭数据刷新
		 */
		public function stopUpdate():void
		{
			if (!isUpdate) return;
			isUpdate = false;
			Globals.gameManager.delDataUpdate(update);
		}
		
		private function update():void 
		{
			_roleDataManager.update();
			_buildDataManager.update();
			_flagDataManager.update();
			
			
			if (isLastWave && !isSendEndGame)
			{
				if (_roleDataManager.hasRole)
				{
					print("最后一波怪，游戏结束");
					isSendEndGame = true;
					//Globals.timeManager.addDelay(5, timeGameEndHandler);
					timeGameEndHandler();
				}
			}
		}
		
		private function timeGameEndHandler():void 
		{
			print("发送游戏结束的消息");
			sendMsg(NotificationName.UPDATE_RAIDINFO, null, NotificationRaidInfo.END_GAME);
		}
		/**
		 * 添加一个行动者到地图中
		 * @param	p_actor
		 */
		public function addActor(p_base:EnergyData, p_role:BaseRole):void
		{
			_roleDataManager.addActor(p_base, p_role, IAstarFindPath(_aStar));
		}
		

		/**
		 * 删除一个行动者
		 * @param	p_id
		 */
		public function delActor(p_soleId:int):void
		{
			_roleDataManager.delActor(p_soleId);
		}
		/**
		 * 角色收到伤害的消息
		 * @param	msgHitActor
		 */
		public function hitActor(msgHitActor:MsgHitActor):void 
		{
			_roleDataManager.hitActor(msgHitActor);
		}
		
		private function initBuildList():void 
		{
			_buildDataManager.initBuildList(_mMapData.baseBuilds);
		}

		public function addBuilding(p_baseBuild:BaseBuild, p_cellX:int, p_cellY:int, skills:Vector.<BaseSkill>, p_soleId:int):void
		{
			_buildDataManager.addBuilding(p_baseBuild, p_cellX, p_cellY, skills, p_soleId);
		}
		/**
		 * 升级建筑物
		 * @param	p_id
		 */
		public function upgradeBuilding(p_soleId:int, p_baseBuild:BaseBuild, p_baseFlag:BaseFlag , skills:Vector.<BaseSkill>):void
		{
			_buildDataManager.upgradeBuilding(p_soleId, p_baseBuild, p_baseFlag, skills);
		}
		/**
		 * 销毁建筑物
		 * @param	p_id
		 */
		public function destoryBuilding(p_soleId:int):void
		{
			_buildDataManager.destoryBuilding(p_soleId, _mMapData.baseBuilds);
		}
		
		public function addFlag(p_baseFlag:BaseFlag, p_x:int, p_y:int):void
		{
			_flagDataManager.addFlag(p_baseFlag, p_x, p_y);
		}
		
		public function updateFlag(p_baseFlag:BaseFlag):void
		{
			_flagDataManager.updateFlag(p_baseFlag);
		}
		
		public function delFlag(p_soleId:int):void
		{
			_flagDataManager.delFlag(p_soleId);
		}
		public function removeFlag(p_flag:MFlagInfo):void
		{
			_flagDataManager.removeFlag(p_flag);
		}

		public function dispose():void 
		{
			_roleDataManager.dispose();
			_buildDataManager.dispose();
			_flagDataManager.dispose();
			
			_mMapData.dipose();
			isSendEndGame = false;
			isLastWave = false;
		}
		/**
		 * 更新地图数据ByteArray
		 * @param	data
		 */
		public function updateMapData(data:ByteArray, raidId:int):void
		{
			sendMsg(NotificationName.FIGHT_MAP_MANAGER, null, NotificationFight.REGAIN_GAME);
			_mMapData.formatMapData(data);
			this.raidId = raidId;
			Globals.lib.parse([Globals.domain.format(Globals.domain.background + _mMapData.mapRes, ResType.T_SWF)], { onComplete:completeHandler } );
		}
		
		public function addBuff(p_roleId:int, baseBuff:BaseBuff):void
		{
			_roleDataManager.addBuff(p_roleId, baseBuff);
		}

		public function delBuff(roleId:int, baseId:int):void
		{
			_roleDataManager.delBuff(roleId, baseId);
		}
		
		/**
		 * 关卡所有资源加载完毕分派
		 * @param	e
		 */
		private function completeHandler(e:LoaderEvent):void 
		{
			sendNotification(NotificationName.LOADER_MAP_COMPLETE,  Globals.lib.getResObj(Globals.domain.format(Globals.domain.map + raidId, ResType.T_XML)), NotificationName.LOADER_ENERGY_COMPLETE);
			sendNotification(NotificationName.CHANGE_SCENE, SceneType.GAME_SCENE);
			sendNotification(NotificationName.FIGHT_NOTIFICATION, _mMapData.mapRes, NotificationFight.CHANGE_FIGHT);
			//sendNotification(NotificationFight.CHANGE_FIGHT, _mMapData.mapRes);
			//sendNotification(NotificationName.FIGHT_MAP_MANAGER, _mMapData.baseBuilds, NotificationFight.INIT_BUILD_LIST);
			initBuildList();
			isLastWave = false;
			
		}
	}
}